// import {
//   getContentModalTable,
//   editContentModalRecord
// } from '../../api/content'
import {
  CONTENT_MODAL_LIST,
  CONTENT_MODAL_CURRENT_ROW,
  CONTENT_MODAL_DATA_COUNT,
  CONTENT_MODAL_HISTORY_DATA,
  CONTENT_MODAL_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    contentModalList: [],
    contentModalDataCount: null,
    contentModalPageSize: 10,
    contentModalHistoryData: [],
    row: {
      title: '',
      startTime: '',
      endTime: '',
      description: '',
      img: '',
      link: '',
      openState: false,
      state: ''
    }
  },
  getters: {
    contentModalList: state => state.contentModalList
  },
  mutations: {
    [CONTENT_MODAL_LIST] (state, data) {
      state.contentModalList = data
    },
    [CONTENT_MODAL_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [CONTENT_MODAL_DATA_COUNT] (state, data) {
      state.contentModalDataCount = data
    },
    [CONTENT_MODAL_HISTORY_DATA] (state, data) {
      state.contentModalHistoryData = data
    },
    [CONTENT_MODAL_PAGE_SIZE] (state, data) {
      state.contentModalPageSize = data
    }
  },
  actions: {
    async getContentModalList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      for (let i = 32; i > 0; i--) {
        newData = [{
          name: '注册领好礼',
          ctime: '2018/12/10',
          btime: '2018/12/10',
          etime: '2019/12/30',
          content: '引导绑定手机号',
          img: '',
          link: '',
          state: 1,
          openState: true
        }, ...newData]
      }
      // const resp = await getContentModalTable(data)
      // console.log(resp)
      // resp.data.data.rows.map((item) => {
      //   newData = [...newData, {
      //     id: item.id,
      //     name: item.name,
      //     ctime: item.ctime,
      //     btime: item.btime,
      //     etime: item.etime,
      //     content: item.content,
      //     img: item.img,
      //     link: item.link,
      //     openState: item.state === 1 ? true : false,
      //     state: item.state
      //   }]
      // })
      commit(CONTENT_MODAL_LIST, newData)
      commit(CONTENT_MODAL_DATA_COUNT, newData.length)
      if (newData.length < rootState.contentModal.contentModalPageSize) {
        commit(CONTENT_MODAL_HISTORY_DATA, newData)
      } else {
        commit(CONTENT_MODAL_HISTORY_DATA, newData.slice(0, rootState.contentModal.contentModalPageSize))
      }
    },
    async editContentModalData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      await editContentModalRecord(data)
    },
    async getContentModalCurrentRow ({ commit, rootState }, row) {
      commit(CONTENT_MODAL_CURRENT_ROW, row)
    },
    async changeContentModalPage ({ commit, rootState }, index) {
      let data = rootState.contentModal.contentModalList
      let _start = (index - 1) * rootState.contentModal.contentModalPageSize
      let _end = index * rootState.contentModal.contentModalPageSize
      commit(CONTENT_MODAL_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeContentModalPageSize ({ commit, rootState }, value) {
      commit(CONTENT_MODAL_PAGE_SIZE, value)
    }
  }
}
